﻿using WCell.Constants.Spells;
using WCell.RealmServer.Misc;

namespace WCell.RealmServer.Formulas
{
	/// <summary>
	/// RageGenerator
	/// <see href="http://www.wowwiki.com/Formulas:Rage_generation"></see>
	/// </summary>
	public static class RageGenerator
	{
		public delegate void RageCalculator(AttackAction action);

		public static RageCalculator GenerateAttackerRage = GenerateDefaultAttackerRage;

		public static RageCalculator GenerateTargetRage = GenerateDefaultTargetRage;

		public static void GenerateDefaultAttackerRage(AttackAction action)
		{
			var attacker = action.Attacker;

			// only generate Rage for white damage
			if (action.IsWeaponAttack)
			{
				int hitFactor;
				if (action.Weapon == attacker.OffHandWeapon)
				{
					hitFactor = 7;
				}
				else
				{
					hitFactor = 14;
				}
				if (action.IsCritical)
				{
					hitFactor *= 2;
				}

				hitFactor *= action.Weapon.AttackTime;

				var lvl = attacker.Level;
				var c = 0.0092f * lvl * lvl + 3.23f * lvl + 4.27f;

				var rage = ((15 * action.ActualDamage / c) + (hitFactor / 2000f)) + 1;
				attacker.Power += (int)rage;
			}
		}

		public static void GenerateDefaultTargetRage(AttackAction action)
		{
			var target = action.Victim;

			var lvl = target.Level;
			var c = (int)(0.0092 * lvl * lvl + 3.23f * lvl + 4.27f);
			target.Power += (5 * (action.ActualDamage + 1) / (2 * c));
		}
	}
}
